package com.scraper;

import java.util.ArrayList;
import java.util.List;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * This class is responsible for retrieving pertinent data from web pages associated with course listings.
 * 
 * @author Micah Perry 
 */
public class CourseListingWebScraper extends WebScraper{
	
	/**
	 * The data object for the current instance of the CourseListingWeScraper.
	 */
	public CourseListingWebData webData = new CourseListingWebData(); 

	/**
	 * This method determines which scrape method to call depending on the current ScraperType.
	 * @param targetDocument The Document representing the HTML to be scraped.
	 */
	void scrapeWebPage(Document targetDocument) {
		switch(this.scraperType){
			case CourseListing: 
				scrapeCourseListing(targetDocument);
				break;
			case CurrentCourseListings:
				scrapeCurrentCourseListings(targetDocument);
				break;
			case CollegeCourseListing:
				scrapeCollegeCourseListing(targetDocument);
				break;
			default:
		} 
	}
	
	/**
	 * This method scrapes the current course listings webpage to retrieve pertinent data. 
	 * <p>
	 * In particular, 
	 * it will return a collection of links representing the available course listings. It also retrieves
	 * the archived course listings URL.
	 * <p>
	 * Example webpage: <a href="http://www.uah.edu/cgi-bin/schedule.pl">http://www.uah.edu/cgi-bin/schedule.pl</a>
	 * 
	 * @param courseListingsDocument The Document representing the HTML to be scraped. 
	 * @return List of course listing links.
	 */
	public List<Link> scrapeCurrentCourseListings(Document courseListingsDocument){
		
		// Retrieve Course Listing links
		String linkQuery = "ul li a";
		Elements courseLinks = courseListingsDocument.select(linkQuery);
		
		List<Link> returnCourseListings = new ArrayList<Link>();
		
		for(Element element : courseLinks){
			Link courseListingLink = WebScraperCommon.constructLinkGivenTag(element); 
			returnCourseListings.add(courseListingLink); 
		}
		
		// Retrieve archived URL
		String archivedQuery = "p a"; 
		Elements archivedLinkElement = courseListingsDocument.select(archivedQuery);
		Link archivedLink = null;
		for(Element element : archivedLinkElement){
			
			String href = WebScraperCommon.retrieveElementAbsoluteHref(element);
			
			if(href.contains("archived")){
				archivedLink = WebScraperCommon.constructLinkGivenTag(element);
			}
		}

		// Update the WebData object
		webData.archivedCourseListingsURL = archivedLink.url; 
		webData.currentCourseListings = returnCourseListings; 
		
		return returnCourseListings;
	}
	
	/**
	 * This method scrapes a particular course listing webpage to retrieve pertinent data. 
	 * <p>
	 * In particular, it will return a collection of links representing the available college course listings.
	 * <p>
	 * Example webpage: <a href="http://www.uah.edu/cgi-bin/schedule.pl?file=sprg2013.html&amp;segment=NDX">http://www.uah.edu/cgi-bin/schedule.pl?file=sprg2013.html&amp;segment=NDX</a>
	 * <p>
	 * <pre> Example data:
		<a href="/cgi-bin/schedule.pl?file=sprg2013.html&segment=ACC">ACC</a>   -Accounting                                          
		<a href="/cgi-bin/schedule.pl?file=sprg2013.html&segment=ARH">ARH</a>   -Art History                                         
		<a href="/cgi-bin/schedule.pl?file=sprg2013.html&segment=ARS">ARS</a>   -Art Studio                                          
		<a href="/cgi-bin/schedule.pl?file=sprg2013.html&segment=AST">AST</a>   -Astronomy                                           
		<a href="/cgi-bin/schedule.pl?file=sprg2013.html&segment=ATS">ATS</a>   -Atmospheric Science                                 
		<a href="/cgi-bin/schedule.pl?file=sprg2013.html&segment=BLS">BLS</a>   -Business Legal Studies                              
		<a href="/cgi-bin/schedule.pl?file=sprg2013.html&segment=BSE">BSE</a>   -Biotechnology Science & Engine                      
		<a href="/cgi-bin/schedule.pl?file=sprg2013.html&segment=BYS">BYS</a>   -Biological Sciences                                 
		<a href="/cgi-bin/schedule.pl?file=sprg2013.html&segment=CE" >CE</a>    -Civil Engineering                                   
		<a href="/cgi-bin/schedule.pl?file=sprg2013.html&segment=CH" >CH</a>    -Chemistry                                           
		<a href="/cgi-bin/schedule.pl?file=sprg2013.html&segment=CHE">CHE</a>   -Chemical Engineering                                
		<a href="/cgi-bin/schedule.pl?file=sprg2013.html&segment=CM" >CM</a>    -Communication Arts                                  
		<a href="/cgi-bin/schedule.pl?file=sprg2013.html&segment=CPE">CPE</a>   -Computer Engineering                                
		<a href="/cgi-bin/schedule.pl?file=sprg2013.html&segment=CS" >CS</a>    -Computer Science                                                                         
		</pre> 
	 * @param courseListingDocument The Document representing the HTML to be scraped. 
	 * @return List of college course listings for a particular course listing.
	 */
	public List<Link> scrapeCourseListing(Document courseListingDocument) {

		// Retrieve college course listing links
		String linkQuery = "pre a";	
		Elements collegeLinks = courseListingDocument.select(linkQuery);
		
		List<Link> returnCollegeCourseListings = new ArrayList<Link>();
		for(Element element : collegeLinks){
			
			Link collegeListingLink = WebScraperCommon.constructLinkGivenTag(element); 
			
			if(collegeListingLink.url != "")
				returnCollegeCourseListings.add(collegeListingLink); 
		}
		
		// Update the WebData object 
		webData.collegeCourseListings = returnCollegeCourseListings;
		
		return returnCollegeCourseListings;
	}

	/**
	 * This method scrapes a particular college course listing webpage to retrieve pertinent data. 
	 * <p>
	 * In particular, it will return the course data associated with a particular college course listing.
	 * <p>
	 * Example webpage: <a href="http://www.uah.edu/cgi-bin/schedule.pl?file=sprg2013.html&amp;segment=CS">http://www.uah.edu/cgi-bin/schedule.pl?file=sprg2013.html&amp;segment=CS</a>
	 * 
	 * @param spring2013csCourseListing The Document representing the HTML to be scraped. 
	 * @return The course data for a particular college course listing. 
	 */
	public String scrapeCollegeCourseListing(Document spring2013csCourseListing) {
		
		String returnCourseData = ""; 
		
		String linkQuery = "pre";
		
		Elements collegeData = spring2013csCourseListing.select(linkQuery);
		
		for(Element element : collegeData){
			
			returnCourseData = element.text();
		}
		
		// Update the WebData object 
		webData.courseListingdata = returnCourseData; 
		
		return returnCourseData;
	}
	
	/**
	 * This method scrapes the archived course listing webpage to retrieve pertinent data.  
	 * <p>
	 * In particular, it will return a collection of links representing the available archived course listings.
	 * <p>
	 * Example webpage: <a href="http://www.uah.edu/cgi-bin/schedule.pl?dir=archived ">http://www.uah.edu/cgi-bin/schedule.pl?dir=archived </a>
	 * 
	 * @param archivedCourseListingDocument The Document representing the HTML to be scraped. 
	 * @return List of Links to archived course listings.
	 */
	public List<Link> scrapeArchivedCourseListing(
			Document archivedCourseListingDocument) {
		
		List<Link> archivedCourseListings = new ArrayList<Link>();
		
		String linkQuery = "ul li a";
				
		Elements archivedLinks = archivedCourseListingDocument.select(linkQuery);
		
		for(Element element : archivedLinks){
			
			Link archivedeListingLink = WebScraperCommon.constructLinkGivenTag(element); 
			
			if(archivedeListingLink.url != "")
				archivedCourseListings.add(archivedeListingLink); 
		}
		
		// Update the WebData object 
		webData.archivedCourseListings = archivedCourseListings;
		
		return archivedCourseListings;
	}
	
	/**
	 * This method returns the WebData object. 
	 * 
	 * @return The WebData object associated with the current instance of WebScraper.
	 */
	public CourseListingWebData getScrapedData(){
		return this.webData;
	}

}
